// DlgSetAccount.cpp
//

// #include "../QeubeeApp.h"
#include "DlgAccCheck.h"

#include <uam/UserAccountManager.h>

#include <QColor>
#include <QDialog>
#include <QPainter>
#include <QPoint>
#include <QRect>
#include <QString>

CDlgAccCheck::CDlgAccCheck(QWidget* pParent /*=NULL*/) : SPopupWindow(pParent) {
  setWindowFlag(Qt::FramelessWindowHint);
  setAttribute(Qt::WA_TranslucentBackground);
  setFixedSize(420, 416);

  setTitle(tr("申请试用"));
  m_imgButton = ImgMgr::instance().image("guiframe_button_default");

  m_lfContent = font();
  m_lfContent.setBold(true);
  m_lfContent.setPixelSize(13);

  m_btnOK = m_btnCancel = NULL;
  m_chAgree = NULL;
  m_bAgreed = false;

  CreateButton();

  m_chAgree = CreateCheck(tr("本人承诺身份信息真实有效"));

  connect(m_chAgree, &SCheckEx::stateChanged, this,
          &CDlgAccCheck::OnCheckBtnDown);
  connect(m_btnOK, &QPushButton::clicked, this, &CDlgAccCheck::OnBtnOk);
  connect(m_btnCancel, &QPushButton::clicked, this, &CDlgAccCheck::OnBtnCancel);
}

CDlgAccCheck::~CDlgAccCheck() {}

void CDlgAccCheck::resizeEvent(QResizeEvent* event) {
  QRect R = rect();
  int nGap = 20;
  QRect rcOK, rcCancel;
  if (m_imgButton) {
    rcOK.setLeft(R.left() + (R.width() - m_imgButton->width() * 2 - nGap) / 2);
    rcOK.setTop(R.bottom() - nGap - m_imgButton->height());
    rcOK.setRight(rcOK.left() + m_imgButton->width());
    rcOK.setBottom(R.bottom() - nGap);
    rcCancel = rcOK;
    rcCancel.adjust(m_imgButton->width() + nGap, 0, m_imgButton->width() + nGap,
                    0);

    if (m_btnOK != NULL) m_btnOK->move(rcOK.left(), rcOK.top());
    if (m_btnCancel != NULL) m_btnCancel->move(rcCancel.left(), rcCancel.top());
  }

  if (m_chAgree) {
    QFontMetrics fontMetrics(m_lfContent);
    int nFontWidth = fontMetrics.width(tr("本人承诺身份信息真实有效"));
    int nW = nFontWidth + 16 /*check按钮大小*/ + 4 + 8 + 4 /*边距*/;
    QRect rcCheck;
    int nGap = (R.width() - nW) / 2;
    rcCheck.setLeft(R.left() + nGap);
    rcCheck.setTop(rcOK.top() - 70);
    m_chAgree->move(rcCheck.left(), rcCheck.top());
  }
}

void CDlgAccCheck::DrawMain() {
  QPainter painter(this);

  CUserAccountInfo& ui = SSUserAccountManager::instance().GetUserInfo();

  QRect m_rcMain = rect();
  QColor brNormal = NAME_COLOR("TextNormal");
  QColor brAtte = QColor(230, 179, 66);

  QString strTemp = tr("请核对您的职位信息是否正确：");
  int X = m_rcMain.left() + 25;
  int Y = m_rcMain.top() + 24 /*标题*/ + 25;
  painter.setFont(QFont(m_lfContent));

  QFontMetrics fontMetrics(m_lfContent);
  int nFontWidth = fontMetrics.width(strTemp);
  int nFontHeight = fontMetrics.height();
  QRect rcText = QRect(X, Y, nFontWidth, nFontHeight);
  painter.setPen(QPen(brNormal));
  painter.drawText(rcText, strTemp,
                   QTextOption(Qt::AlignRight | Qt::AlignVCenter));

  int lineheight = 14;
  X = m_rcMain.left() + m_rcMain.width() / 3;
  Y += lineheight * 3;
  strTemp = tr("姓名：");
  nFontWidth = fontMetrics.width(strTemp);
  nFontHeight = fontMetrics.height();
  rcText = QRect(X - nFontWidth, Y, nFontWidth, nFontHeight);
  painter.setPen(QPen(brNormal));
  painter.drawText(rcText, strTemp,
                   QTextOption(Qt::AlignRight | Qt::AlignVCenter));

  strTemp = QString::fromLocal8Bit(ui.m_DisplayName);
  nFontWidth = fontMetrics.width(strTemp);
  nFontHeight = fontMetrics.height();
  rcText = QRect(X + 10, Y, nFontWidth, nFontHeight);
  painter.setPen(QPen(brAtte));
  painter.drawText(rcText, strTemp,
                   QTextOption(Qt::AlignRight | Qt::AlignVCenter));

  Y += lineheight * 2;
  strTemp = tr("机构：");
  nFontWidth = fontMetrics.width(strTemp);
  nFontHeight = fontMetrics.height();
  rcText = QRect(X - nFontWidth, Y, nFontWidth, nFontHeight);
  painter.setPen(QPen(brNormal));
  painter.drawText(rcText, strTemp,
                   QTextOption(Qt::AlignRight | Qt::AlignVCenter));

  strTemp = QString::fromLocal8Bit(ui.m_CompanyName);
  nFontWidth = fontMetrics.width(strTemp);
  nFontHeight = fontMetrics.height();
  rcText = QRect(X + 10, Y, nFontWidth, nFontHeight);
  painter.setPen(QPen(brAtte));
  painter.drawText(rcText, strTemp,
                   QTextOption(Qt::AlignRight | Qt::AlignVCenter));

  Y += lineheight * 2;
  strTemp = tr("邮箱：");
  nFontWidth = fontMetrics.width(strTemp);
  nFontHeight = fontMetrics.height();
  rcText = QRect(X - nFontWidth, Y, nFontWidth, nFontHeight);
  painter.setPen(QPen(brNormal));
  painter.drawText(rcText, strTemp,
                   QTextOption(Qt::AlignRight | Qt::AlignVCenter));

  strTemp = QString::fromLocal8Bit(ui.m_UserAccount);
  nFontWidth = fontMetrics.width(strTemp);
  nFontHeight = fontMetrics.height();
  rcText = QRect(X + 10, Y, nFontWidth, nFontHeight);
  painter.setPen(QPen(brAtte));
  painter.drawText(rcText, strTemp,
                   QTextOption(Qt::AlignRight | Qt::AlignVCenter));

  Y += lineheight * 2;
  strTemp = tr("电话：");
  nFontWidth = fontMetrics.width(strTemp);
  nFontHeight = fontMetrics.height();
  rcText = QRect(X - nFontWidth, Y, nFontWidth, nFontHeight);
  painter.setPen(QPen(brNormal));
  painter.drawText(rcText, strTemp,
                   QTextOption(Qt::AlignRight | Qt::AlignVCenter));

  strTemp = QString::fromLocal8Bit(ui.m_Telephone);
  int nL = strTemp.indexOf(tr(";"));
  if (nL > 0) strTemp = strTemp.left(nL);
  nFontWidth = fontMetrics.width(strTemp);
  nFontHeight = fontMetrics.height();
  rcText = QRect(X + 10, Y, nFontWidth, nFontHeight);
  painter.setPen(QPen(brAtte));
  painter.drawText(rcText, strTemp,
                   QTextOption(Qt::AlignRight | Qt::AlignVCenter));

  Y += lineheight * 2;
  strTemp = tr("手机：");
  nFontWidth = fontMetrics.width(strTemp);
  nFontHeight = fontMetrics.height();
  rcText = QRect(X - nFontWidth, Y, nFontWidth, nFontHeight);
  painter.setPen(QPen(brNormal));
  painter.drawText(rcText, strTemp,
                   QTextOption(Qt::AlignRight | Qt::AlignVCenter));

  strTemp = QString::fromLocal8Bit(ui.m_Mobile);
  nL = strTemp.indexOf(tr(";"));
  if (nL > 0) strTemp = strTemp.left(nL);
  nFontWidth = fontMetrics.width(strTemp);
  nFontHeight = fontMetrics.height();
  rcText = QRect(X + 10, Y, nFontWidth, nFontHeight);
  painter.setPen(QPen(brAtte));
  painter.drawText(rcText, strTemp,
                   QTextOption(Qt::AlignRight | Qt::AlignVCenter));

  Y = m_rcMain.bottom() - 80;
  strTemp = tr("如您的信息有误，请拨打 400-119-9898 与我们联系");
  nFontWidth = fontMetrics.width(strTemp);
  nFontHeight = fontMetrics.height();
  X = m_rcMain.left() + (m_rcMain.width() - nFontWidth) / 2;
  strTemp = tr("如您的信息有误，请拨打 ");
  nFontWidth = fontMetrics.width(strTemp);
  nFontHeight = fontMetrics.height();
  rcText = QRect(X, Y, nFontWidth, nFontHeight);
  painter.setPen(QPen(brNormal));
  painter.drawText(rcText, strTemp,
                   QTextOption(Qt::AlignRight | Qt::AlignVCenter));
  X += nFontWidth;
  strTemp = tr("400-119-9898");

  nFontWidth = fontMetrics.width(strTemp);
  nFontHeight = fontMetrics.height();
  rcText = QRect(X, Y, nFontWidth, nFontHeight);
  painter.setPen(QPen(brAtte));
  painter.drawText(rcText, strTemp,
                   QTextOption(Qt::AlignRight | Qt::AlignVCenter));
  X += nFontWidth;

  strTemp = tr(" 与我们联系");
  nFontWidth = fontMetrics.width(strTemp);
  nFontHeight = fontMetrics.height();
  rcText = QRect(X + 10, Y, nFontWidth, nFontHeight);
  painter.setPen(QPen(brNormal));
  painter.drawText(rcText, strTemp,
                   QTextOption(Qt::AlignRight | Qt::AlignVCenter));
}

void CDlgAccCheck::paintEvent(QPaintEvent* event) {
  QPainter painter(this);

  QRect r = rect();
  painter.setRenderHint(QPainter::Antialiasing);
  painter.setBrush(QBrush(StyleMgr::instance().color("BasicPlate")));
  QPen pen1(StyleMgr::instance().color("InnerBorder"), 1);
  painter.setPen(pen1);
  painter.drawRoundedRect(r, StyleMgr::instance().size("RoundCorner"),
                          StyleMgr::instance().size("RoundCorner"));

  SPopupWindow::paintEvent(event);

  DrawMain();
}

void CDlgAccCheck::CreateButton() {
  m_btnOK = new QPushButton("提交", this);
  QString style =
      QString(
          "QPushButton{background:#%1; "
          "border-radius:0px;color:#FFE5C7}\nQPushButton:hover{background:#%2}"
          "\nQPushButton:disabled{color:#585B5A;background:#263735}\n"
          "#%1{background:#0C4F44}\n}")
          .arg(StyleMgr::instance().color("ButtonNormal").rgb(), 0, 16)
          .arg(StyleMgr::instance().color("Hover").rgb(), 0, 16);

  m_btnOK->setStyleSheet(style);
  m_btnOK->setFixedSize(80, 24);
  m_btnOK->setEnabled(false);

  m_btnCancel = new QPushButton("取消", this);
  m_btnCancel->setStyleSheet(style);
  m_btnCancel->setFixedSize(80, 24);
}

SCheckEx* CDlgAccCheck::CreateCheck(QString strText) {
  SCheckEx* pCheck = new SCheckEx(tr("本人承诺身份信息真实有效"), this);
  pCheck->setTextBk(false);
  pCheck->setFixedSize(188, 21);
  return pCheck;
}

void CDlgAccCheck::OnBtnOk() { close(); }

void CDlgAccCheck::OnBtnCancel() { close(); }

void CDlgAccCheck::OnCheckBtnDown() {
  m_bAgreed = (m_chAgree->isSelected() == true) ? 1 : 0;

  if (m_btnOK) {
    if (m_bAgreed)
      m_btnOK->setEnabled(true);
    else
      m_btnOK->setEnabled(false);
  }

  return;
}
